

#include <stdio.h>
#include <string.h>
#include "iotv30_config.h"
#include "rtc_calendar.h"

#include "user_flash.h"

#include "time_taskv30.h"




static uint8_t u_time_task_save_flag = 0;


//uint8_t time_task_num;

time_task_manager_t   time_task_manager;
time_task_t time_task_list[TIME_TASK_MAX];




static void time_task_default(void)
{
    memset( (uint8_t*)&time_task_manager.First ,0, sizeof(time_task_manager_t)  );
    memset( (uint8_t*)&time_task_list[0] ,0, sizeof(time_task_t)*TIME_TASK_MAX);    

    time_task_manager.First = FLASH_TIME_TASK_FIRST_WR;

    time_task_save_enable( );
}


void time_task_save_enable(void)
{
    u_time_task_save_flag = 1;
}



/**
 * @brief time_task_init
 * 
 */
void time_task_init(void)
{

  FILE * pFile = NULL;

  logout( "%s\n",__FUNCTION__ );

    memset( (uint8_t*)&time_task_manager.First ,0, sizeof(time_task_manager_t)  );
    memset( (uint8_t*)&time_task_list[0] ,0, sizeof(time_task_t)*TIME_TASK_MAX);    


     pFile = File_Unlock( TIME_TASK_FILE_NAME ) ;

  //文件操作
	if (  NULL == pFile)
	{// //打开失败---文件不存在
		logout( "%s fail! open file %s fail!\n",__FUNCTION__,TIME_TASK_FILE_NAME );

	}else{
        ReadDataFromFlash_8Bits(pFile, (uint8_t*)&time_task_manager.First,FLASH_TIME_TASK_MANAGER_ADDR,sizeof(time_task_manager_t) );  
        ReadDataFromFlash_8Bits(pFile,(uint8_t*)&time_task_list[0],FLASH_TIME_TASK_PAGE,sizeof(time_task_t)*TIME_TASK_MAX);  

        if( FLASH_TIME_TASK_FIRST_WR != time_task_manager.First ) 
        {//第一次上电
            time_task_default( );           
        }

      File_Lock( pFile);

	}		

}



/**
 * @brief 保存定时任务,添加/删除等操作后
 */
void Time_Taskv30_Save_Process(void)
{

  FILE * pFile;

	 if( 0== u_time_task_save_flag )
     {
		  return;
     }
	 
	 u_time_task_save_flag = 0;

		 //打开文件
         pFile = File_Unlock( TIME_TASK_FILE_NAME ) ;


	   //文件操作
		if (  NULL != pFile)
		{//

            WriteDataToFlash_8Bits(pFile,  (uint8_t*)&(time_task_manager.First),FLASH_TIME_TASK_MANAGER_ADDR,sizeof(time_task_manager_t) );  	
            WriteDataToFlash_8Bits(pFile,  (uint8_t*)&time_task_list[0],FLASH_TIME_TASK_PAGE,sizeof(time_task_t)*((uint16_t)(time_task_manager.total)) );         
//          WriteDataToFlash_8Bits(pFile,  (uint8_t*)&time_task_list[0],FLASH_TIME_TASK_PAGE,sizeof(time_task_t)*TIME_TASK_MAX); //保存所有定时任务数据        

            File_Lock(pFile); 

		}else{ //打开失败---文件不存在

            logout( "%s fail!\n",__FUNCTION__ );
		}

}




/**
 * @brief 添加一个定时任
 * @param task 
 * @return uint8_t 
 */
uint8_t time_task_add(time_task_t* task)
{
 uint8_t i=0;
 uint8_t time_task_num = time_task_get_num( );
   
    for(; i < time_task_num; i++)
    {
        if(time_task_list[i].task_id == task->task_id)
        {
            break;// 找到相同uid，结束循环，直接覆盖该任  
        }
    }

    if(i < TIME_TASK_MAX)
    {
        memcpy(&time_task_list[i],task,sizeof(time_task_t));

        if( i==time_task_num ) {   // 如果是末尾添加，则更新任务数
           time_task_num++;
           time_task_set_num(time_task_num );
        }
        return 1;
    }
    return 0;
}

/**
 * @brief 删除一个定时任
 * @param task_id 
 * @return uint8_t 
 */
uint8_t time_task_del(uint8_t task_id)
{
uint8_t time_task_num = time_task_get_num( );

    for(uint8_t i=0;i<time_task_num;i++)
    {
        if(time_task_list[i].task_id == task_id)
        {
            memmove(&time_task_list[i],&time_task_list[i+1],sizeof(time_task_t)*(time_task_num-i-1));
            memset(&time_task_list[time_task_num-1],0,sizeof(time_task_t));
            time_task_num--;

            time_task_set_num(time_task_num );

            return 1;
        }
    }
    return 0;
}

/**
 * @brief 删除所有定时任�? * 
 */
void time_task_del_all(void)
{
    time_task_default( );  

   // time_task_num = 0;
  //  memset(time_task_list,0,sizeof(time_task_t)*TIME_TASK_MAX);
}



/**
 * @brief 
 * 
 * @return uint8_t 
 */
uint8_t time_task_get_num(void)
{
    return time_task_manager.total;
}



/**
 * @brief 
 * 
 * @return uint8_t 
 */
bool time_task_set_num(uint8_t num)
{
     if( num>TIME_TASK_MAX )
     {
        return false;
     }

    time_task_manager.total = num;
    time_task_save_enable( );

    return  true;
}





/**
 * @brief 
 * 
 * @param index 
 * @return time_task_t* 
 */
time_task_t* time_task_get_task(uint8_t index)
{
    if(index < time_task_get_num( ) )
    {
      return &time_task_list[index];
    }

return NULL;

}




